package samsung.sds.survey.surveyManagement.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.departmentManagement.model.DepartmentEntity;
import samsung.sds.departmentManagement.service.DepartmentManagementService;
import samsung.sds.survey.surveyManagement.model.SurveryCheckEntity;
import samsung.sds.survey.surveyManagement.model.SurveyEntity;
import samsung.sds.survey.surveyManagement.model.SurveyUserEntity;
import samsung.sds.survey.surveyManagement.service.SurveyReleaseService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;

/**
 * @author tingcai.liu
 * @createDate 2016-01-15
 * 
 *  问卷发布的Controller
 */
@Controller
@RequestMapping("/survey/surveyManagement/surveyRelease")
public class SurveyReleaseController {

	@Autowired
	private SurveyReleaseService surveyReleaseServiceImpl;

	@Autowired
	private DepartmentManagementService departmentManagementServiceImpl;

	/**
	 * 跳转到已选择用户列表界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/loadSurveyRelease_sitemesh_popup", method = RequestMethod.GET)
	public String loadSurveyRelease() {
		return "/survey/surveyManagement/surveyReleasePopup";
	}

	/**
	 * 跳转到用户组页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toGroup_sitemesh_popup", method = RequestMethod.GET)
	public String toGroup() {
		return "/survey/surveyManagement/surveyReleaseGroup";
	}

	/**
	 * 跳转到组织结构页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toDepart_sitemesh_popup", method = RequestMethod.GET)
	public String toDepart() {
		return "/survey/surveyManagement/surveyReleaseDepart";
	}

	/**
	 * 查询所有组织机构并初始化ztree
	 */	
	@RequestMapping(value = "/queryDepartmentListBySurveyId", method = RequestMethod.POST)
	@ResponseBody
	public List<DepartmentEntity> queryDepartmentListBySurveyId(
			HttpServletRequest request, String surveyId) 
	{
		List<DepartmentEntity> departmentEntitys = new ArrayList<DepartmentEntity>();
		QueryParam param = new QueryParam();

		String surveyId1 = request.getParameter("surveyId");
		departmentEntitys = departmentManagementServiceImpl.queryList(param);
		List<SurveryCheckEntity> list = surveyReleaseServiceImpl.queryUserCheckbox(surveyId1);
		
		for (int i = 0; i < departmentEntitys.size(); i++) {
			for (int j = 0; j < list.size(); j++) {
				if (departmentEntitys.get(i).getDeptId().toString()
						.equals(list.get(j).getDepartId())){
					if (list.get(j).getFlag() == 0) {
						departmentEntitys.get(i).setCheckStatus("H");
					   } else if (list.get(j).getFlag() == 1) {
						departmentEntitys.get(i).setCheckStatus("A");
					   } else if (list.get(j).getFlag() == -1) {
						departmentEntitys.get(i).setCheckStatus("N");
					   }
				 } 
			 }
		}
		return departmentEntitys;
	}
	
	
	
	
	
	/**
	 * 分页获取用户列表,按照条件查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getUserListByParameter", method = RequestMethod.POST)
	@ResponseBody
	public PageData<SurveyUserEntity> getUserListByParameter(
			HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		return surveyReleaseServiceImpl.queryListPage(param);
	}

	 /**
	  * 根据surveyId，checkedDeptIds，nocheckedDeptIds对变更用户进行增删操作
	  * @param request
	  * @param checkedDeptIds
	  * @param nocheckedDeptIds
	  * @return
	  */
	@RequestMapping(value = "/submitCheckedUser", method = RequestMethod.POST)
	@ResponseBody
	public String submitCheckedUser(HttpServletRequest request,
			String checkedDeptIds, String nocheckedDeptIds) {

		String checkedDepartIds1 = request.getParameter("checkedDeptIds");
		String nocheckedDepartIds1 = request.getParameter("nocheckedDeptIds");
		String surveyId = request.getParameter("surveyId");

		int isInsertSuccess = surveyReleaseServiceImpl.insertDeptUser(surveyId, checkedDepartIds1);
		int isDeleteSuccess = surveyReleaseServiceImpl.deleteDeptUser(surveyId, nocheckedDepartIds1);
		
		// 返回值为1，成功，否则，失败
		if (isInsertSuccess == 1 && isDeleteSuccess == 1) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
		
	}

	
	/**
	 * 删除用户对特定调查问卷的权限
	 * 
	 * @param request
	 * @param usernames
	 * @return
	 */
	  @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	  @ResponseBody 
	  public String deleteUser(HttpServletRequest request, String usernames) { 
	  //接收页面传递的参数 
	  String survey_id = request.getParameter("survey_id");
	  
	  int isSuccess = surveyReleaseServiceImpl.deleteUser(survey_id,usernames);
	  
	  //返回值为1，成功，否则，失败 
	  if(isSuccess == 1){
		  return Constants.SUCCESS_CODE;
	    }else{ 
		  return Constants.FAILURE_CODE; 
	   } 
	 }
	 
	/**
	 * 发布调查问卷
	 * 
	 * @param survey
	 * @return
	 */
	@RequestMapping(value = "/surveyRelease", method = RequestMethod.POST)
	@ResponseBody
	public String surveyRelease(SurveyEntity survey) {
		// 问卷发布
		int isSuccess = surveyReleaseServiceImpl.surveyRelease(survey);

		// 返回值为1，成功，否则，失败
		if (isSuccess == 1) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	   * 更改问卷发布的对象用户
	   * @param request
	   * @return
	   */
	  @RequestMapping(value = "/updateGroupUserToSurveyUserList", method = RequestMethod.POST)
	  @ResponseBody
	  public ArrayList<String> updateGroupUserToSurveyUserList(HttpServletRequest request,SurveyUserEntity surveyUser) {

	    //接收页面传递的参数
	    String groupForInsert  = request.getParameter("groupForInsert");
	    String groupForDelete  = request.getParameter("groupForDelete");
	    String survey_id = request.getParameter("survey_id");
	    String create_user = request.getParameter("create_user");
	    int insertUserCount  = 0;
	    int delUserCount  = 0;
	    
	    //整租删除
	    delUserCount = surveyReleaseServiceImpl.deleteGroupUserToSurveyUserList(groupForDelete,survey_id);
	    
	    //整组插入
	    insertUserCount = surveyReleaseServiceImpl.insertGroupUserToSurveyUserList(groupForInsert,survey_id,create_user);
	    
	    ArrayList<String> counts = new ArrayList<String>();
	    counts.add(0, String.valueOf(insertUserCount));
	    counts.add(1, String.valueOf(delUserCount));

		return counts;
		
	  }
	  
	  /**
	   * 查询group人员投票权限状态
	   * @param request
	   * @param map
	   * @return
	   */
	  @RequestMapping(value="/selectGroupList",method=RequestMethod.POST)
	  @ResponseBody
	  public List<SurveyUserEntity> selectUserGroupList(HttpServletRequest request ) {
	    // 获取当前系统用户
	    String createUser = request.getParameter("createUser");
	    String surveyId   = request.getParameter("surveyId");
	    List<SurveyUserEntity> groupList = surveyReleaseServiceImpl.selectGroupList(createUser,surveyId );
	    
	    return groupList;
	  }
}
